﻿
Partial Class App_Default
    Inherits MyUI.PageBaseApp

    Protected CurrAccount As mDB.Account

    Protected c As Web.UI.Control
    Protected AlertLastId As Integer


#Region "PAGE"

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim u = MyApp.GetUser(True)

        If u IsNot Nothing AndAlso u.Tab_User_Account.ToList.Count = 0 And Me.Request.Url.AbsolutePath.IndexOf("useraccounts.aspx", StringComparison.OrdinalIgnoreCase) = -1 Then
            'Response.Redirect("~/App/useraccounts.aspx")
        End If


        'UpPAlert.ID = "notification"
    End Sub
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then
            SetListAccount()
            SetListNewAlert(False)
            SetUserMessage()
        Else
            SetCurrAccount()
        End If


        'Me.AddJScript("var links = Array;")
        'Me.AddJScript("links['DASHBOARD'] = '" & UI.Route.MakeUrl("DASHBOARD", Nothing) & "';")
        'Me.AddJScript("links['AUTO_FOLLOW'] = '" & UI.Route.MakeUrl("FOLLOW", Nothing) & "';")
        'Me.AddJScript("links['LISTEN'] = '" & UI.Route.MakeUrl("LISTEN", Nothing) & "';")
        'Me.AddJScript("links['CONNECTIONS'] = '" & UI.Route.MakeUrl("CONNECTIONS", Nothing) & "';")
        'Me.AddJScript("links['SOCIAL_SEO'] = '" & UI.Route.MakeUrl("SOCIAL_SEO", Nothing) & "';")


        'Me.AddJScript("links['SCHEDULE'] = '" & UI.Route.MakeUrl("SCHEDULE", Nothing) & "';")
        'Me.AddJScript("links['FOLLOWERS'] = '" & UI.Route.MakeUrl("FOLLOWERS", Nothing) & "';")
        ''Me.AddJScript("links['FOLLOWING'] = '" & UI.Route.MakeUrl("FOLLOWING", Nothing) & "';")
        'Me.AddJScript("links['HASHTAG'] = '" & UI.Route.MakeUrl("HASHTAG", Nothing) & "';")
        ''Me.AddJScript("links['CONVERSATIONS'] = '" & UI.Route.MakeUrl("CONVERSATIONS", Nothing) & "';")


        'Me.AddJScript("links['USERACCOUNT'] = '/App/UserAccounts.aspx';")
        'Me.AddJScript("links['BILLING'] = '/App/Billing.aspx';")
        'Me.AddJScript("links['FollowersOverview'] = '/App/FollowersOverview.aspx';")
        '

        If CurrUser IsNot Nothing Then
            Me.HTML_MeunAdmin.Visible = CurrUser.Id = 1
        Else
        End If

        If CurrAccount Is Nothing Then
            'CurrAccount = New mDB.Account
            PnlAccount.Visible = False
            UpPBarScroll.Visible = False
            'CurrAccount.SetDefault()
        End If

    End Sub
    Protected Sub TimerUpdate_Tick(sender As Object, e As EventArgs) Handles TimerUpdate.Tick

        Dim tempcount = Me.BtnViewAlert.Text

        If CurrUser.Session.CurrPage = "LISTEN" Then
            CurrAccount.DownloadAccountEngadment(False)
            If CurrUser.Session.Listen.Box.Contains(3) Then
                If CurrAccount.DownloadAccountHashTag() Then
                    tempcount = ""
                End If
            End If
        End If

        SetListNewAlert(False)

        If Me.BtnViewAlert.Text <> tempcount Then
            Me.AddJScript("refresh_frame_data();")
        End If

    End Sub

#End Region

#Region "USER"

    Private Sub SetUserMessage()

        If CurrUser.CurrUserPlan.Plan_Expire_Date < Now Then
            Me.PnlUserMessage.Visible = True
            Me.LnkUserMessage.Text = "piano scaduto"
        End If

        If Not CurrUser.Email_Confirm Then
            Me.PnlUserMessage.Visible = True
            Me.LnkUserMessage.Text = "Account activation email not yet confirmed. Click here to send the activation email again."
            Me.LnkUserMessage.CommandName = "SEND_EMAIL_CONFIRM"
        End If


    End Sub
    Protected Sub LnkUserMessage_Click(sender As Object, e As EventArgs) Handles LnkUserMessage.Click

        Select Case sender.CommandName
            Case "SEND_EMAIL_CONFIRM"
                'MyApp.Email.Send.User.ConfirmEmail(CurrUser)
                MyApp.Email.System.Queued(MyApp.Email.System.Type.USER_CONFIRM_EMAIL, True, New With {.user_id = CurrUser.Id})
                Me.LnkUserMessage.Enabled = False
                Me.LnkUserMessage.Text = "Email sent!"
                'Me.AddJScript("")

        End Select

    End Sub

#End Region





#Region "ALERT"

    Private Sub SetListNewAlert(update As Boolean)

        Dim LastId As Integer = Utils.Conv.ToInt(CurrUser.Session.Alert.LastViewID)

        Dim NewAlert = CurrUser.Tab_User_Alert.
            Where("alert_id", ">", LastId).
            SetSelect("count(*) as count,max(alert_id) as alert_id").
        ToDataTable

        Dim AlertCount = NewAlert(0)("count")
        Dim AlertMaxID = NewAlert(0)("alert_id")


        If update Then
            If AlertCount <> 0 Then
                CurrUser.Session.Alert.LastViewID = AlertMaxID
            End If
            AlertCount = 0
        End If

        If AlertCount = 0 Then
            UI.Controls.ClassRemove(Me.BtnViewAlert, "yes-notify")
            Me.BtnViewAlert.Text = "-"
        Else
            UI.Controls.ClassAdd(Me.BtnViewAlert, "yes-notify")
            If AlertCount > 99 Then
                Me.BtnViewAlert.Text = "99+"
            Else
                Me.BtnViewAlert.Text = AlertCount

            End If

        End If



        Me.UpPAlertCount.Update()

    End Sub
    Private Sub SetListAlert()
        Dim dt As DataTable

        dt = CurrUser.View_Alert.
            REL.Alert.Account.
            REL.Alert.FromAccount.
            OrderBy("alert_id desc").
            SetSelect("user_alert.* ", "alert.*", "account.*",
                "from_account.username as from_username", "from_account.url as from_url", "from_account.avatar as from_avatar").
            Limit(10).
        ToDataTable()

        UI.ListData.Bind(Me.ListMessage, dt)

        Me.UpPAlert.Update()

    End Sub
    'Private Sub DownloadAccountEngadment(Reload As Boolean)

    '    If CurrAccount.Account_Update.Message_Post_Update < Now.AddSeconds(-60 * 2) Or Reload Then

    '        Dim tw = New MyApp.Network.Twitter(CurrAccount.GetOauth)
    '        tw.DownloadAccountMessagePosts(CurrAccount)

    '        tw.DownloadAccountFavoritePosts(CurrAccount)
    '        'tw.DownloadAccountShares(CurrAccount)
    '        tw.DownloadAccountPrivatePosts(CurrAccount)
    '        tw.DownloadAccountPosts(CurrAccount, GloukDB.Account.UpdatePostMode.FOLLOWER)

    '        'IMPORTANTE sarebbbe da ottimizare solo per l'account
    '        MyApp.Task.Update.AccountMessagesConversations()

    '        MyApp.Task.Update.AccountAccountsUpdate(New List(Of mDB.Account) From {CurrAccount}, GloukDB.Account.UpdateAccountMode.ENGAGEMENT)

    '    End If

    'End Sub

    Protected Sub ListMessage_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListMessage.ItemCommand

        Response.Redirect(SF("/App/redirect.aspx?u={0}&p={1}", e.CommandArgument, e.CommandName), True)

    End Sub

    Protected Sub BtnViewAlert_Click(sender As Object, e As EventArgs) Handles BtnViewAlert.Click


        SetListAlert()

    End Sub
    Protected Sub BtnAlertRead_Click(sender As Object, e As EventArgs) Handles BtnAlertRead.Click

        SetListNewAlert(True)


    End Sub

#End Region


#Region "ACCOUNT"

    Public Sub SetCurrAccount()

        CurrAccount = Me.CurrUser.View_EnabledAccount.GetByPK(Utils.Conv.ToInt(Me.DdlAccount.SelectedValue))

        If CurrAccount IsNot Nothing Then
            'CurrUser.Session(mDB.User.SessionKey.CURR_ACC) = CurrAccount.Id
            CurrUser.Session.CurrAccount = CurrAccount.Id
        End If

    End Sub
    Private Sub SetListAccount()

        Dim dt = Me.CurrUser.View_EnabledAccount.ToDataTable ' 

        UI.List.Bind(Me.DdlAccount, dt, "fullname", "id", CurrUser.Session.CurrAccount, False)
        UI.List.Bind(Me.DdlAccount2, dt, "fullname", "id", CurrUser.CurrGroup.Account_Ids, False)

        For ct = 0 To Me.DdlAccount.Items.Count - 1
            Dim i = Me.DdlAccount.Items(ct)  'As ListItem 
            Dim dr = dt.Rows(ct)
            i.Attributes("data-image") = dr("avatar")
            i.Attributes("data-description") = dr("description")
        Next

        Me.ChkGroupEnabled.Checked = CurrUser.Session.ActiveGroup
        Me.PnlGroup.Visible = Me.ChkGroupEnabled.Checked

        SetCurrAccount()

    End Sub

    Protected Sub DdlAccount_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DdlAccount.SelectedIndexChanged

        SetListAccount()

    End Sub
    Protected Sub BtnLogout_Click(sender As Object, e As System.EventArgs) Handles BtnLogout.Click

        Dim AU = New MyUI.AuthUsers()
        AU.LogOut()

        Response.Redirect("~/App/")

    End Sub
    Protected Sub BtnReload_Account_Click(sender As Object, e As EventArgs) Handles BtnReload_Account.Click

        Me.UpPAccount.Update()
        Me.UpPBarScroll.Update()

    End Sub

    Protected Sub ChkGroupEnabled_CheckedChanged(sender As Object, e As EventArgs) Handles ChkGroupEnabled.CheckedChanged
        CurrUser.Session.ActiveGroup = Me.ChkGroupEnabled.Checked
        SetListAccount()
    End Sub
    Protected Sub BtnMultiAcc_Click(sender As Object, e As System.EventArgs) Handles BtnMultiAcc.Click

        CurrUser.CurrGroup.Account_Ids = UI.List.GetSelectValuesInt(Me.DdlAccount2) '(From s In Request("DdlAccount2").ToString.Split(",") Select CInt(s)).ToList

    End Sub

#End Region









End Class